package com.syedu.day16;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * description：Java字符输入流
 * @author Administrator
 * @date 2022/4/7 13:39
 */
public class ReaderDemo {
    public static void main(String[] args) {
        /**BIO*/
        //此方式是每次读取一个字符,效率较低
        try (Reader reader = new FileReader("D:\\BAM练习.txt")) {
            int len = -1;
            //循环读取字符
            while ((len = reader.read()) != -1) {
                System.out.print((char) len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //使用带缓冲的字符流每次读取一整行内容,从而提高读取效率
        try (BufferedReader reader = new BufferedReader(
                new FileReader("D:\\BAM练习.txt"))) {
            String line = null;
            //循环读取每一行
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        /**NIO,JDK7.0开始可以使用*/
        try {
            List<String> stringList = Files.readAllLines(Path.of("D:\\BAM练习.txt"));
            stringList.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /**第三方框架*/
        //1.guava
        /*try {
            List<String> stringList = Files.readLines(new File("D:\\BAM练习.txt"), Charset.defaultCharset());
            stringList.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        //2.hutool
        /*List<String> stringList = FileUtil.readLines("D:\\BAM练习.txt", "UTF-8");
        stringList.forEach(System.out::println);*/

        //3.commons-io
        /*try {
            List<String> stringList = FileUtils.readLines(new File("D:\\BAM练习.txt"), "UTF-8");
            stringList.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}